package com.sneaker.shower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sneaker.shower.common.ThreadLocalCache;
import com.sneaker.shower.entity.domain.order.discount.CouponTransactionDO;
import com.sneaker.shower.mapper.CouponTransactionMapper;
import com.sneaker.shower.service.CouponTransactionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Hechangjian
 * @version 1.0
 * @className CouponTransactionServiceImpl
 * @date 2019/11/26 23:07
 * @description
 * @program bath
 */
@Service
@Slf4j
public class CouponTransactionServiceImpl extends ServiceImpl<CouponTransactionMapper, CouponTransactionDO>
        implements CouponTransactionService {

    /**
     * 一张优惠券有且只有一条待使用或已使用的记录
     * 可能有多条已取消的记录
     *
     * @param couponGuid 优惠券guid
     * @return
     */
    @Override
    public List<CouponTransactionDO> getTransactionByCouponGuid(Long couponGuid) {
        LambdaQueryWrapper<CouponTransactionDO> wrapper = new LambdaQueryWrapper<CouponTransactionDO>()
                .eq(CouponTransactionDO::getUserGuid, Long.valueOf(ThreadLocalCache.get().getGuid()))
                .eq(CouponTransactionDO::getCouponGuid, couponGuid);
        return this.list(wrapper);
    }

    @Override
    public List<CouponTransactionDO> getUserCouponTransaction(Integer couponType) {
        LambdaQueryWrapper<CouponTransactionDO> wrapper = new LambdaQueryWrapper<CouponTransactionDO>()
                .eq(CouponTransactionDO::getUserGuid, Long.valueOf(ThreadLocalCache.get().getGuid()));
        if (couponType != null) {
            wrapper.eq(CouponTransactionDO::getCouponType, couponType);
        }
        return list(wrapper);
    }

    @Override
    public Boolean createTransaction(CouponTransactionDO couponTransactionDO) {
        return this.save(couponTransactionDO);
    }
}

